// JavaScript Document
var time_out;
var city_lock=0;
var city_lock_id=-1;
var obj;
var num_per_page = 100;
var xhr;
function getLocalTime(nS) {     
   return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
}     
function display_result(data){
	$('#search-result-wrapper').empty();
	obj = JSON.parse(data);
	var all_s = '';
	if(obj.length==0){
		$('#search-result-wrapper').append('<div class="row"><div class="large-6 columns search-result" style="margin-bottom:20px;margin-left:12px;">未找到任何结果。请尝试调整你的搜索条件</div></div>');	
		return;
	}
	var exact_match = obj[0].exact_match;
	if(0){
		$('#search-result-wrapper').append('<div class="row"><div class="large-6 columns search-result" style="margin-bottom:20px;margin-left:12px;">未找到100%符合条件的结果。请尝试调整你的搜索条件</div></div>');	
	}
	for(var i=0;i<Math.min(num_per_page, obj.length);++i){
		var descrip = obj[i].descrip;
		var profile = obj[i].profile;
		var dinner = obj[i].dinner;
		var islogin = obj[i].islogin;
		var md = obj[i].matching_degree.toFixed(2)+'%';
		var profile_image = profile['image'];
		var profile_link = base_url_user + 'profile/u/'+profile['user_id'];
		var name = profile['name'];
		var status = profile['status'];
		var recent_status = decode_university[profile['recent_condition_university']];
		if(recent_status==''){
			recent_status = profile['recent_condition_workplace'];
		}
		var from = '来自'+decode_from[profile['from']];
		var prefix = '';
		var postfix = '';
		switch(profile['rent_sex'])
		{
			case '1':
			rent_sex='男女皆可';
			break;
			case '2':
			rent_sex='男性';
			break;
			case '3':
			rent_sex='女性';
			break;
			default:
			rent_sex='无';
		}
		all_s = all_s + prefix +
		'<div class="" style="margin-top:10px; margin-right: 10px; display: inline-block; background:#ffffff;"><div class="th radius" style="height: 402px;width: 230px; ">'+
		'<a target="_blank" href="'+profile_link+'"><img style="width:222px; height:222px;" src="'+profile_image+'"></a>'+
		'<div style="margin: 12px; height:28px; overflow:hidden;">'+
		'<h5><a target="_blank" href="'+profile_link+'" style="color:#623B17">'+name+'</a></h5>'+
		'</div>'+
		'<div style="margin: 10px; height: 75px; overflow: hidden;">';
		all_s = all_s + '<span class="label radius solid from-place" style="margin:2px;">'+decode_sex[profile['sex']]+'</span>';
		if(profile['from']==2){
			all_s = all_s + '<span class="label radius solid" style="margin:2px;">来自'+decode_from_p[profile['from_p']]+'</span>';
		}else if(from !='来自'){
			all_s = all_s + '<span class="label radius solid from-place" style="margin:2px;">来自'+from+'</span>';
		}
		if(profile['sex']){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">性别要求：<a title="'+rent_sex+'">'+rent_sex+'</a></div>';
		}
		if(descrip[3]){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">期望区域：<a title="'+descrip[3]+'">'+descrip[4]+'</a></div>';
		}
		if(descrip[1]){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">个人期望：<a title="'+descrip[1]+'">'+descrip[2]+'</a></div>';
		}
		if(profile['last_login']){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">最后登录：<a title="'+getLocalTime(profile['last_login'])+'">'+getLocalTime(profile['last_login'])+'</a></div>';
		}
		all_s = all_s + '</div>'+	
		'<div style="margin-left:10px;margin-right:10px;">';
		
		all_s = all_s + '<a class="label button radius" target="_blank" style="margin:2px; margin-right:4px;" href="'+base_url_user+'messageboard/u/'+profile['user_id']+'">详情</a>';
		all_s = all_s + '<a href="'+profile_link+'" target="_blank" class="label int-btn secondary button radius invite-user-btn " style="background:#f60; color:#fff; width:auto;" title="邀请该用户合租，邀请被接受后她将成为你的室友"><img class="int-img" src="'+base_url+'assets/img/invite_white.png">邀请合租</a>';
		if(dinner==1){ 
			var din1='';
			var din2='none';
			var din3='none';
		}else if(dinner==2){ 
			var din1='none';
			var din2='';
			var din3='none';
		}else{ 
			var din1='none';
			var din2='none';
			var din3='';
		}
		if(islogin==1){ 
			var hre1='onClick="cannelricegroup(this)"';
			var hre2='onClick="ricegroup(this)"';
		}
		else var hre1=hre2='href="'+base_url+'user/login"';
		/*约团饭功能
		all_s = all_s + '<a id="cancel-rice-group-'+profile['user_id']+'" class="label button radius cancel-rice-group" style="margin:2px; margin-right:4px;display:'+din1+'" t="'+profile['user_id']+'" title="点击取消约'+name+'团饭"><font color="#F90" '+hre1+'>待通过</font></a>';
		all_s = all_s + '<a class="label button radius" style="margin:2px; margin-right:4px;display:'+din2+'" title="'+name+'已通过约团饭"><font color="#F90">已约</font></a>';
		all_s = all_s + '<a id="rice-group-'+profile['user_id']+'" class="label button radius rice-group" style="margin:2px; margin-right:4px;display:'+din3+'" t="'+profile['user_id']+'" title="邀请'+name+'一起吃饭,将发送您的联系方式给对方" '+hre2+'>约团饭</a>';
		*/
		all_s = all_s + '</div>'+'</div></div>';
		all_s = all_s + '</div>'+'</div></div>';
		/*
		'<div class="large-6 columns search-result"><div class="profile-header panel radius">'+
		'<div class="no-padding img-wrapper hide-for-medium hide-for-small"><img class="radius left" src="'+profile_image+
		'"></div><div class="name"><a href="'+profile_link+'" target="_blank">'+name+'</a><span class="label search-result-label">&nbsp;&nbsp;'+md+'符合</span></div>	<div class="status">'+status+
		'</div><div class="tag-holder-2"><span class="label search-result-label">'+recent_status+
		'</span><span class="label search-result-label">'+from+'</span></div></div></div>'+postfix;
		*/
	}
	$('#search-result-wrapper').append(all_s);

}

function display_result2(data){
	$('#search-result-wrapper').empty();
	obj =data;
	var search_tip="已为您搜索到  <span style='color:#0099ff'>"+obj.length+"</span>  位人选";
	//console.log(data);
	var all_s = '';
	var area_str = '';
	if(obj.length==0){
		$('#search-result-wrapper').append('<div class="row"><div class="large-6 columns search-result" style="margin-bottom:20px;margin-left:12px;">未找到任何结果。请尝试调整你的搜索条件</div></div>');	
		return;
	}
	var exact_match = obj[0].exact_match;
	for(var i=0;i<Math.min(num_per_page, obj.length);++i){
		//var descrip = obj[i].descrip;
		var profile = obj[i];
		//alert(profile);
		//var dinner = obj[i].dinner;
		//var islogin = obj[i].islogin;
		//var md = obj[i].matching_degree.toFixed(2)+'%';
		var profile_image = profile['image'];
		var profile_link = base_url_user + 'profile/u/'+profile['user_id'];
		var name = profile['name'];
		var status = profile['status'];
		var recent_status = decode_university[profile['recent_condition_university']];
		if(recent_status==''){
			recent_status = profile['recent_condition_workplace'];
		}
		var from = '来自'+decode_from[profile['from']];
		var prefix = '';
		var postfix = '';
		var rent_sex='无';
		switch(profile['rent_sex'])
		{
			case '1':
			rent_sex='男女皆可';
			break;
			case '2':
			rent_sex='男性';
			break;
			case '3':
			rent_sex='女性';
			break;
			default:
			rent_sex='无';
		}
		var rs = profile['recentsoncity_id'];
		// rs_array=rs.split(",");
		all_s = all_s + prefix +
		'<div class="" style="margin-top:10px; margin-right: 10px; display: inline-block; background:#ffffff;"><div class="th radius" style="height: 402px;width: 230px; ">'+
		'<a target="_blank" href="'+profile_link+'"><img style="width:222px; height:222px;" src="'+profile_image+'"></a>'+
		'<div style="margin: 12px; height:28px; overflow:hidden;">'+
		'<h5><a target="_blank" href="'+profile_link+'" style="color:#623B17">'+name+'</a></h5>'+
		'</div>'+
		'<div id="append_area_'+i+'" style="margin: 10px; height: 75px; overflow: hidden;">';


		all_s = all_s + '<span class="label radius solid from-place" style="margin:2px;">'+decode_sex[profile['sex']]+'</span>';
		if(profile['from']==2){
			all_s = all_s + '<span class="label radius solid" style="margin:2px;">来自'+decode_from_p[profile['from_p']]+'</span>';
		}else if(from !='来自'){
			all_s = all_s + '<span class="label radius solid from-place" style="margin:2px;">'+from+'</span>';
		}
		 if(profile['rent_sex']){
		 	all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">性别要求：<a title="'+rent_sex+'">'+rent_sex+'</a></div>';
		 }
		 if(profile['last_login']){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">最后登录：<a title="'+getLocalTime(profile['last_login'])+'">'+getLocalTime(profile['last_login'])+'</a></div>';
		}

		if(typeof rs == 'string' && rs != undefined && rs != null & rs != ''){
		all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">期望区域：';
			var rs_array = rs.split(",");
			var alerttt = '';
			 for (var j = 0; j < rs_array.length-1; j++) {
			 	var data = rs_array[j]; 
			 	all_s = all_s + '<a title="'+data+'"  class="area_'+data+'"></a>';
			}

		all_s = all_s + '</div>';	
			//ajax--
		area_str +=  rs_array;	


	      
		}
		if(profile['rent_period']){
			all_s = all_s + '<div style="color:#2f7f8c;word-wrap: break-word;line-height: 25px;" size="2">入住期长：<a title="'+profile['rent_period']+'">'+profile['rent_period']+' 个月</a></div>';
		 }
		all_s = all_s + '</div>'+	
		'<div style="margin-left:10px;margin-right:10px;">';
		
		all_s = all_s + '<a class="label button radius" target="_blank" style="margin:2px; margin-right:4px;" href="'+base_url_user+'profile/u/'+profile['user_id']+'">详情</a>';
		all_s = all_s + '<a href="'+base_url_user+'profile/invite/'+profile['user_id']+'" target="_blank" class="label int-btn secondary button radius invite-user-btn " style="background:#f60; color:#fff; width:auto;" title="邀请该用户合租，邀请被接受后她将成为你的室友"><img class="int-img" src="'+base_url+'assets/img/invite_white.png">直接组队</a>';
		// if(dinner==1){ 
		// 	var din1='';
		// 	var din2='none';
		// 	var din3='none';
		// }else if(dinner==2){ 
		// 	var din1='none';
		// 	var din2='';
		// 	var din3='none';
		// }else{ 
		// 	var din1='none';
		// 	var din2='none';
		// 	var din3='';
		// }
		// if(islogin==1){ 
		// 	var hre1='onClick="cannelricegroup(this)"';
		// 	var hre2='onClick="ricegroup(this)"';
		// }
		// else 
		var hre1=hre2='href="'+base_url+'user/login"';
		/*约团饭功能
		all_s = all_s + '<a id="cancel-rice-group-'+profile['user_id']+'" class="label button radius cancel-rice-group" style="margin:2px; margin-right:4px;display:'+din1+'" t="'+profile['user_id']+'" title="点击取消约'+name+'团饭"><font color="#F90" '+hre1+'>待通过</font></a>';
		all_s = all_s + '<a class="label button radius" style="margin:2px; margin-right:4px;display:'+din2+'" title="'+name+'已通过约团饭"><font color="#F90">已约</font></a>';
		all_s = all_s + '<a id="rice-group-'+profile['user_id']+'" class="label button radius rice-group" style="margin:2px; margin-right:4px;display:'+din3+'" t="'+profile['user_id']+'" title="邀请'+name+'一起吃饭,将发送您的联系方式给对方" '+hre2+'>约团饭</a>';
		*/
		all_s = all_s + '</div>'+'</div></div>';
		all_s = all_s + '</div>'+'</div></div>';
		/*
		'<div class="large-6 columns search-result"><div class="profile-header panel radius">'+
		'<div class="no-padding img-wrapper hide-for-medium hide-for-small"><img class="radius left" src="'+profile_image+
		'"></div><div class="name"><a href="'+profile_link+'" target="_blank">'+name+'</a><span class="label search-result-label">&nbsp;&nbsp;'+md+'符合</span></div>	<div class="status">'+status+
		'</div><div class="tag-holder-2"><span class="label search-result-label">'+recent_status+
		'</span><span class="label search-result-label">'+from+'</span></div></div></div>'+postfix;
		*/
	}


	var data = {'area_id':area_str}; 
			$.ajax({
	            url:'/roommate/get_area',
	            data:data,
	            type:'POST',
	            dataType:'text',
	            success:function(msg){
	            var obj = JSON.parse(msg);
	            // var data = msg.text;
           		for(var k=0;k<obj.length;k++){
           			// alerttt += ' '+obj[k].cityname; 
           		$('.area_'+obj[k].id).text(obj[k].cityname+' '); 
           		} 
        		}
	              
	        });
	$('#search-result-wrapper').append(all_s);
	//$('#search_tip').append(search_tip);

}


function search_main(save){
//the first function for user search
var sex = '1';
var sex_ = $('#sex-selection').find('.select');
for(var i=0;i<sex_.length;++i){
	sex +=sex_.get(i).getAttribute('t');
}
var age = '0';
var age_ = $('#age-selection').find('.select');
for(var i=0;i<age_.length;++i){
	age +=age_.get(i).getAttribute('t');
}
	//var f = '';
	var from = '0';
	var from_ = $('#from-selection').find('.select');
	for(var i=0;i<from_.length;++i){
		//from +=from_.get(i).getAttribute('t');
		if(from_.get(i).getAttribute('t')==1) from+=from_.get(i).getAttribute('cid')+",";
	}
	var one_p = 0;
	var which_p = 0;
	//var fp = '';
	var from_p = '0';//first province in decode is ''
	var from_p_ = $('#from-p-selection').find('.select');
	for(var i=0;i<from_p_.length;++i){
		//from_p +=from_p_.get(i).getAttribute('t');
		if(from_p_.get(i).getAttribute('t')==1){
			from_p+=from_p_.get(i).getAttribute('cid')+",";
			++one_p;	
			which_p = i;
		}
	}
	//var fc='';
	var from_c = '0';
	var from_c_;
	++which_p;
	if(one_p==1){//if only one provice is selected, we collect city info
		from_c_ = $('#from-c-selection-'+which_p).find('.select');
		for(var i=0;i<from_c_.length;++i){
			//from_c +=from_c_.get(i).getAttribute('t');
			if(from_c_.get(i).getAttribute('t')==1) from_c+=from_c_.get(i).getAttribute('cid')+",";
		}
	}
	var type = '0';
	var type_ = $('#type-selection').find('.select');
	for(var i=0;i<type_.length;++i){
		type +=type_.get(i).getAttribute('t');
	}
	var university = '0';
	var university_;
	var study_mode = '0';
	var study_mode_;
	var degree = '0';
	var degree_;
	university_ = $('#university-selection').find('.select');
	for(var i=0;i<university_.length;++i){
		university +=university_.get(i).getAttribute('t');
	}
	study_mode_ = $('#study-mode-selection').find('.select');
	for(var i=0;i<study_mode_.length;++i){
		study_mode +=study_mode_.get(i).getAttribute('t');
	}
	degree_ = $('#degree-selection').find('.select');
	for(var i=0;i<degree_.length;++i){
		degree +=degree_.get(i).getAttribute('t');
	}
	var relationship_status = '0';
	var relationship_status_ = $('#relationship-status-selection').find('.select');
	for(var i=0;i<relationship_status_.length;++i){
		relationship_status +=relationship_status_.get(i).getAttribute('t');
	}
	var relationship_deal = '0';
	var relationship_deal_ = $('#relationship-deal-selection').find('.select');
	for(var i=0;i<relationship_deal_.length;++i){
		relationship_deal +=relationship_deal_.get(i).getAttribute('t');
	}
	var habit_1 = '';
	var habit_1_ = $('#habit-1-selection').find('.select');
	for(var i=0;i<habit_1_.length;++i){
		habit_1 +=habit_1_.get(i).getAttribute('t');
	}
	var habit_2 = '';
	var habit_2_ = $('#habit-2-selection').find('.select');
	for(var i=0;i<habit_2_.length;++i){
		habit_2 +=habit_2_.get(i).getAttribute('t');
	}
	var skill_1 = '';
	var skill_1_ = $('#skill-1-selection').find('.select');
	for(var i=0;i<skill_1_.length;++i){
		skill_1 +=skill_1_.get(i).getAttribute('t');
	}
	var skill_2 = '';
	var skill_2_ = $('#skill-2-selection').find('.select');
	for(var i=0;i<skill_2_.length;++i){
		skill_2 +=skill_2_.get(i).getAttribute('t');
	}
	var skill_3 = '';
	var skill_3_ = $('#skill-3-selection').find('.select');
	for(var i=0;i<skill_3_.length;++i){
		skill_3 +=skill_3_.get(i).getAttribute('t');
	}
	var hobby_1 = '';
	var hobby_1_ = $('#hobby-1-selection').find('.select');
	for(var i=0;i<hobby_1_.length;++i){
		hobby_1 +=hobby_1_.get(i).getAttribute('t');
	}
	var hobby_2 = '';
	var hobby_2_ = $('#hobby-2-selection').find('.select');
	for(var i=0;i<hobby_2_.length;++i){
		hobby_2 +=hobby_2_.get(i).getAttribute('t');
	}
	var hobby_3 = '';
	var hobby_3_ = $('#hobby-3-selection').find('.select');
	for(var i=0;i<hobby_3_.length;++i){
		hobby_3 +=hobby_3_.get(i).getAttribute('t');
	}
	var hobby_4 = '';
	var hobby_4_ = $('#hobby-4-selection').find('.select');
	for(var i=0;i<hobby_4_.length;++i){
		hobby_4 +=hobby_4_.get(i).getAttribute('t');
	}
	var hobby_5 = '';
	var hobby_5_ = $('#hobby-5-selection').find('.select');
	for(var i=0;i<hobby_5_.length;++i){
		hobby_5 +=hobby_5_.get(i).getAttribute('t');
	}
	var hobby_6 = '';
	var hobby_6_ = $('#hobby-6-selection').find('.select');
	for(var i=0;i<hobby_6_.length;++i){
		hobby_6 +=hobby_6_.get(i).getAttribute('t');
	}

	var object = '';
	var object_ = $('#object-selection').find('.select');
	for(var i=0;i<object_.length;++i){
		object +=object_.get(i).getAttribute('t');
	}
	var rent_start_y = $('#rent-start-y').val();
	var rent_start_m = $('#rent-start-m').val();
	var rent_price = $('#rent-price').val();
	var rent_period = $('#rent-period').val();
	var keyword = $('#keyword').val();
	xhr = $.ajax({
		type: 'POST',
		url: base_url_user+'roommate/pro', 
		data: {
			save: save,
			rent_start_y: rent_start_y,
			rent_start_m: rent_start_m,
			rent_price: rent_price,
			rent_period: rent_period,
			sex: sex,
			age: age,
			from: from,
			from_p: from_p,
			from_c: from_c,
			object: object,
			type: type,
			university: university,
			study_mode: study_mode,
			degree: degree,
			relationship_status: relationship_status,
			relationship_deal: relationship_deal,
			habit_1: habit_1,
			habit_2: habit_2,
			skill_1: skill_1,
			skill_2: skill_2,
			skill_3: skill_3,
			hobby_1: hobby_1,
			hobby_2: hobby_2,
			hobby_3: hobby_3,
			hobby_4: hobby_4,
			hobby_5: hobby_5,
			hobby_6: hobby_6,
			keyword:keyword
		},
		dataType: 'text',
		success: function(data){
				// if(save=='1'){
				// }
				// else{
				// 	html_span_l = '<span class="label button radius not-selected select">';
				// 	html_span_r = '</span>';
				// 	html_str = '';
				// 	html_str += html_span_l+'预计入住：'+rent_start_y+'年'+rent_start_m+'月'+html_span_r+'&nbsp;';
				// 	var obj = $("body span[t='1']");
				// 	$.each(obj,function(k,v){
				// 		 html_str += html_span_l+$(v).text()+html_span_r+'&nbsp;';
				// 	});
				// 	$('#screen_condition').html(html_str);
				// 	 $("#screen_condition > span").click(function () {
				// 	  var val = $(this).text();
				//       $(this).remove();

				// 	$.each(obj,function(k,v){
				// 		if($(v).text() == val){
				// 			$(v).attr('t','0');
				// 		}
				// 	});
				//     });
//console.log(data);
display_result(data);
				//}
			},  
			cache: false
		});
}



// $('#keyword_search').click(function(){
// 	var keyword=$('#keyword').val().trim();
// 	if(keyword==''){
//     $("#keyword").css("border-color","rgb(255, 102, 0)");
// 	}else{
//     $("#keyword").css("border-color","#cccccc");
// 	$("#mytip").hide();
// 	search_main('0');	//will reload the page
//  }
// });
$('#good_search').click(function(){
	var from_area='';
	if($(this).attr('disabled')!=null){return false;}
	xhr = $.ajax({
		type: 'POST',
		url: base_url_user+'roommate/good_search', 
		data: {
		from_area: from_area
		},
		dataType: 'json',
		success: function(data){
                 //console.log(data);
                 display_result2(data);
             },  
             cache: false
         });
});



$('#mysubmit').click(function(){
	if($(this).attr('disabled')!=null){return false;}
	$("#mytip").hide();
	var rent_price = $('#rent-price').val();
	var rent_start_y = $('#rent-start-y').val();
	var rent_start_m = $('#rent-start-m').val();
	var keyword = $('#keyword').val();
	var sex = $('#sex').val();
	var university = $('#university').val();
	var area2 = $('#area2').val();
	var my_userid = $('#my_userid').val();
	xhr = $.ajax({
		type: 'POST',
		url: base_url_user+'roommate/user_search', 
		data: {
			rent_start_y: rent_start_y,
			rent_start_m: rent_start_m,
			rent_price: rent_price,
			university: university,
			area2: area2,
			sex: sex,
			my_userid:my_userid,
			keyword:keyword	
		},
		dataType: 'json',
		success: function(data){
                 //console.log(data);
                 //alert(data);
                 display_result2(data);
             },  
             cache: false
         });
});

// $('.save-selection-set-btn').click(function(){
// 	if($(this).attr('disabled')!=null){return false;}
// 	search_main('1');	//will reload the page
// });

// $('span').click(function(){
// 	try{xhr.abort();}
// 	catch(err){}
// 	if(!$(this).hasClass('load-more-selection-set-btn')&&!$(this).hasClass('hide-more-selection-set-btn')&&!$(this).hasClass('save-selection-set-btn')&&!$(this).hasClass('more-btn')&&!$(this).hasClass('less-btn')){
// 		clearTimeout(time_out);
// 		if($('#search-result-wrapper').find('#loading-img').length==0){
// 			var margin_height = Math.max(100,$('#search-result-wrapper').height()-32);
// 			$('#search-result-wrapper').empty();
// 			$('#search-result-wrapper').append('<img id="loading-img" src="'+base_url+'assets/img/searching.gif'+'" style="height:32px; width:32px; margin-left:16px;margin-bottom:'+margin_height+'px;">');
// 		}
// 		time_out = setTimeout(function(){
// 			search_main('0');
// 		},1500);
// 		$('.save-selection-set-btn').removeAttr('disabled');
// 		$('.save-selection-set-btn').text('保存本套筛选条件');
// 	}
// });

// $('.select-all-btn').click(function(){
// 	$(this).hide();
// 	$(this).parent().find('.clear-all-btn').css('display','inline-block');
// 	$(this).parent().find('.select').attr('t', 1);
// 	$(this).parent().find('.select').css('background-color', '#f90'); 

// });
// $('.clear-all-btn').click(function(){
// 	$(this).hide();
// 	$(this).parent().find('.select-all-btn').css('display','inline-block');
// 	$(this).parent().find('.select').attr('t', 0);
// 	$(this).parent().find('.select').css('background-color', '#4b9caf'); 
	
// });

// $('.more-btn').click(function(){
// 	$(this).hide();
// 	$(this).parent().find('.less-btn').css('display','inline-block');
// 	$(this).parent().find('.more-selection').css('display','inline-block');
// });

// $('.less-btn').click(function(){
// 	$(this).parent().find('.less-btn').hide();
// 	$(this).parent().find('.more-btn').css('display','inline-block');
// 	$(this).parent().find('.more-selection').hide();
// });

// $('.select').click(function(){
// 	var acti = $(this).attr('t');
// 	if(acti==1){
// 		$(this).attr('t', 0);
// 		$(this).css('background-color', '#4b9caf'); 

// 	}
// 	else{
// 		$(this).attr('t', 1);
// 		$(this).css('background-color', '#f90'); 
		
// 	}
// 	var all_selects = $(this).parent().find('.select');
// 	var all_selected = true;
// 	var all_not_selected = true;
// 	for(var i=0;i<all_selects.length;++i){
// 		if($(this).parent().find('.select').get(i).getAttribute('t')!=1){
// 			all_selected=false;	
// 		} else{
// 			all_not_selected = false;	
// 		}
// 	}
// 	if(all_selected){
// 		$(this).parent().find('.select-all-btn').hide();
// 		$(this).parent().find('.clear-all-btn').css('display','inline-block');
// 	}
// 	if(all_not_selected){
// 		$(this).parent().find('.clear-all-btn').hide();
// 		$(this).parent().find('.select-all-btn').css('display','inline-block');
// 	}
// });


// $('#from-clear').click(function(){
// 	for(var i=1;i<decode_from_p.length;++i){
// 		$('#from-c-selection-'+i).hide();
// 		$('#from-c-selection-'+i).find('.select').attr('t', 0);
// 		$('#from-c-selection-'+i).find('.select').css('opacity','1');
// 		$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=50)'); 
// 	}
// 	city_lock=0;
// 	$('#from-p-selection').hide();
// 	$('#from-p-selection').find('.select').attr('t', 0);
// 	$('#from-p-selection').find('.select').css('opacity','1');
// 	$('#from-p-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// });

// $('#mainland-clear').click(function(){
// 	for(var i=1;i<decode_from_p.length;++i){
// 		$('#from-c-selection-'+i).hide();
// 		$('#from-c-selection-'+i).find('.select').attr('t', 0);
// 		$('#from-c-selection-'+i).find('.select').css('opacity', '1');
// 		$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=100)'); 
// 	}	
// 	city_lock=0;
// });

// $('.from-select').click(function(){
// 	if($(this).text()==decode_from[2]&&$(this).attr('t')==1){
// 		$('#from-p-selection').show();	
// 	}	
// 	if($(this).text()==decode_from[2]&&$(this).attr('t')==0){
// 		$('#from-p-selection').hide();
// 		$('#from-p-selection').find('.select').attr('t', 0);
// 		$('#from-p-selection').find('.select').css('opacity','1');
// 		$('#from-p-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 		for(var i=1;i<decode_from_p.length;++i){
// 			$('#from-c-selection-'+i).hide();
// 			$('#from-c-selection-'+i).find('.select').attr('t', 0);
// 			$('#from-c-selection-'+i).find('.select').css('opacity','1');
// 			$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=100)'); 
// 		}
// 		city_lock=0;
// 	}	
// });

// $('.mainland-select').click(function(){
// 	if(city_lock!=0){
// 		for(var i=1;i<decode_from_p.length;++i){
// 			$('#from-c-selection-'+i).hide();
// 			$('#from-c-selection-'+i).find('.select').attr('t', 0);
// 			$('#from-c-selection-'+i).find('.select').css('opacity','1');
// 			$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=100)'); 
// 		}	
// 	}
// 	for(var i=1;i<decode_from_p.length;++i){
// 		if($(this).text()==decode_from_p[i]&&$(this).attr('t')==1){
// 			++city_lock;
// 			break;
// 		}
// 		if($(this).text()==decode_from_p[i]&&$(this).attr('t')==0){
// 			$('#from-c-selection-'+i).hide();	
// 			$('#from-c-selection-'+i).find('.select').attr('t', 0);
// 			$('#from-c-selection-'+i).find('.select').css('opacity','1');
// 			$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=100)'); 
// 			--city_lock;
// 			break;
// 		}
// 	}
// 	if(city_lock==1){
// 		for(var i=1;i<decode_from_p.length;++i){
// 			if($('.mainland-select').get(i-1).getAttribute('t')==1){
// 				$('#from-c-selection-'+i).show();
// 			}
// 		}
// 	}
// });

$('#mainland-all').click(function(){
	city_lock=31;
	for(var i=1;i<decode_from_p.length;++i){
		$('#from-c-selection-'+i).hide();
		$('#from-c-selection-'+i).find('.select').attr('t', 0);
		$('#from-c-selection-'+i).find('.select').css('opacity','1');
		$('#from-c-selection-'+i).find('.select').css('filter', 'alpha(opacity=100)'); 
	}	
});

// $('#type-clear').click(function(){
// 	$('#university-selection').hide();
// 	$('#university-selection').find('.select').attr('t', 0);
// 	$('#university-selection').find('.select').css('opacity','1');
// 	$('#university-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 	$('#study-mode-selection').hide();
// 	$('#study-mode-selection').find('.select').attr('t', 0);
// 	$('#study-mode-selection').find('.select').css('opacity','1');
// 	$('#study-mode-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 	$('#degree-selection').hide();
// 	$('#degree-selection').find('.select').attr('t', 0);
// 	$('#degree-selection').find('.select').css('opacity','1');
// 	$('#degree-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// });

// $('.type-select').click(function(){
// 	if($(this).text()==decode_type[1]&&$(this).attr('t')==1){
// 		$('#university-selection').show();
// 		$('#study-mode-selection').show();
// 		$('#degree-selection').show();	
// 	}	
// 	if($(this).text()==decode_type[1]&&$(this).attr('t')==0){
// 		$('#university-selection').hide();
// 		$('#university-selection').find('.select').attr('t', 0);
// 		$('#university-selection').find('.select').css('opacity','1');
// 		$('#university-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 		$('#study-mode-selection').hide();
// 		$('#study-mode-selection').find('.select').attr('t', 0);
// 		$('#study-mode-selection').find('.select').css('opacity','1');
// 		$('#study-mode-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 		$('#degree-selection').hide();
// 		$('#degree-selection').find('.select').attr('t', 0);
// 		$('#degree-selection').find('.select').css('opacity','1');
// 		$('#degree-selection').find('.select').css('filter', 'alpha(opacity=100)'); 
// 	}	
// });

// $('#type-all').click(function(){
// 	$('#university-selection').show();
// 	$('#study-mode-selection').show();
// 	$('#degree-selection').show();	
// });

// $('.load-more-selection-set-btn').click(function(){
// 	if($('.more-selection-set').get(0).getAttribute('loaded')!=null){
// 		$('.more-selection-set').get(1).setAttribute('loaded','1');
// 		$('.more-selection-set').get(1).setAttribute('style','display:block;');
// 		$('.load-more-selection-set-btn').hide();
// 	}	
// 	else{
// 		$('.more-selection-set').get(0).setAttribute('loaded','1');
// 		$('.more-selection-set').get(0).setAttribute('style','display:block;');
// 		$('.hide-more-selection-set-btn').css('display','inline-block');	
// 	}
// });

// $('.hide-more-selection-set-btn').click(function(){
// 	if($('.more-selection-set').get(1).getAttribute('loaded')!=null){
// 		$('.more-selection-set').get(1).removeAttribute('loaded');
// 		$('.more-selection-set').get(1).removeAttribute('style');
// 		$('.load-more-selection-set-btn').css('display','inline-block');	
// 	}
// 	else{
// 		$('.more-selection-set').get(0).removeAttribute('loaded');
// 		$('.more-selection-set').get(0).removeAttribute('style');
// 		$('.hide-more-selection-set-btn').hide();
// 	}
// });
function ricegroup(e){
	var user_id=e.getAttribute("t");
	getricegroup(user_id);
}
$('.rice-group-arr').click(function(){
	var user_id = $('#rice-group-all').val();
	$('#sending').show();
	getricegroup(user_id);
})
function getricegroup(user_id){
	$.ajax({
		type: 'POST',
		url: base_url_user+'dinner/store_dinner', 
		data: {
			user_id: user_id
		},
		success: function(data){
			if(data==-1) alert('已超过可约团饭人数');
			else if(data!=0){
				var strarr=data.split(",");
				var i=0;
				for(i=0;i<strarr.length-1;i++){
					$('#rice-group-'+strarr[i]).hide();
					$('#cancel-rice-group-'+strarr[i]).show();
					if(i>1) $('#sending').html('发送完成');
				}
				popupwindow(strarr.length-1);
			}
		}, 
		dataType: 'text',  
		cache: false
	});
}
function cannelricegroup(e){
	var user_id=e.getAttribute("t");
	$.ajax({
		type: 'POST',
		url: base_url_user+'dinner/cancel_store_dinner', 
		data: {
			user_id: user_id
		},
		success: function(data){
			if(data==-1) popupwindowneed('没有约此人');
			else if(data==1){
				$('#rice-group-'+user_id).show();
				$('#cancel-rice-group-'+user_id).hide();
			}
		}, 
		dataType: 'text',  
		cache: false
	});
}
function popupwindow(num) {
	$('#rp-record').text(parseInt($('#rp-record').text())-parseInt(num));	
	$('#popup').css('width', '210px');
	$('#popup').text("约"+num+"人团饭减"+num+"人品");
	$('#popup').stop(true, true).fadeOut();
	$('#popup').show();
	$('#popup').fadeOut(4000);
}
function popupwindowneed(mess) {	
	$('#popup').css('width', '210px');
	$('#popup').text(mess);
	$('#popup').stop(true, true).fadeOut();
	$('#popup').show();
	$('#popup').fadeOut(4000);
}